package com.spzx.cart.controller;

import com.spzx.cart.api.domain.CartInfo;
import com.spzx.cart.service.ICartService;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.annotation.RequiresLogin;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "购物车接口")
@RestController
@RequestMapping
public class CartController extends BaseController {

    @Autowired
    private ICartService cartService;


    //添加到购物车，前端传入的参数是sku的id，因为我们要对某个商品的sku规格进行添加，并且我们
    @Operation(summary = "添加购物车")
    @RequiresLogin
    @GetMapping("addToCart/{skuId}/{skuNum}")
    public AjaxResult addToCart(@Parameter(name = "skuId", description = "商品skuId", required = true) @PathVariable("skuId") Long skuId,
                                @Parameter(name = "skuNum", description = "数量", required = true) @PathVariable("skuNum") Integer skuNum) {

        cartService.addTocart(skuId,skuNum);
        return success();
    }

    /**
     *
     * 查询购物车
     * @return
     */
    @Operation(summary = "查询购物车")
    @RequiresLogin
    @GetMapping("cartList")
    public AjaxResult cartList() {
        return success(cartService.getCartList());
    }

    /**
     * 删除购物车商品
     *
     * @param skuId
     * @return
     */
    @Operation(summary = "删除购物车商品")
    @RequiresLogin
    @DeleteMapping("deleteCart/{skuId}")
    public AjaxResult deleteCart(@Parameter(name = "skuId", description = "商品skuId", required = true) @PathVariable("skuId") Long skuId) {
        cartService.deleteCart(skuId);
        return success();
    }

    /**
     * 更改商品sku的勾选状态
     * @param skuId
     * @param isChecked
     * @return
     */
    @Operation(summary = "更改商品sku勾选状态")
    @GetMapping("checkCart/{skuId}/{isChecked}")
    public AjaxResult updateCart(@Parameter(name ="skuId",description = "商品skuId",required = true) @PathVariable(value = "skuId") Long skuId,
                                 @Parameter(name="isChecked",description = "商品状态isChecked",required = true) @PathVariable(value = "isChecked") Integer isChecked){
        cartService.updateCart(skuId, isChecked);
        return success();
    }

    /**
     * 更新购物车商品全部的选中状态
     * @param isChecked
     * @return
     */
    @Operation(summary="更新购物车商品全部选中状态")
    @RequiresLogin
    @GetMapping("allCheckCart/{isChecked}")
    public AjaxResult allCheckCart(@Parameter(name = "isChecked", description = "是否选中 1:选中 0:取消选中", required = true) @PathVariable(value = "isChecked") Integer isChecked){
        cartService.allCheckCart(isChecked);
        return success();
    }


    //======为订单/地址模块使用========================================
    @Operation(summary = "查询购物车中被勾选的商品列表")
    @InnerAuth
    @GetMapping("/getCartCheckedList/{userId}")
    public R<List<CartInfo>> getCartCheckedList(@Parameter(name = "userId", required = true)
                                                @PathVariable Long userId){
        return R.ok(cartService.getCartCheckedList(userId));
    }
    /**
     *更新购物车商品列表的价格
     */
    @Operation(summary="更新用户购物车列表价格")
    @InnerAuth
    @GetMapping("/updateCartPrice/{userId}")
    public R<Boolean> updateCartPrice(@PathVariable("userId") Long userId){
        return R.ok(cartService.updateCartPrice(userId));
    }

    @Operation(summary = "下订单后，删除购物车的商品")
    @InnerAuth
    @GetMapping("/deleteCartCheckedList/{userId}")
    public R<Boolean> deleteCartCheckedList(@PathVariable("userId") Long userId){
        return R.ok(cartService.deleteCartCheckedList(userId));
    }
}